package com.gitee.hljdrl.uikit.toolbar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ffsky.logger.L;
import com.gitee.hljdrl.uikit.R;
import com.gitee.hljdrl.uikit.basic.UITitleBarEvent;
import com.gitee.hljdrl.uikit.util.Util;

public class UITitleBar implements UITitleBarEvent {

    public static UITitleBar build(View view) {
        UITitleBar toolbar = new UITitleBar(view);
        return toolbar;
    }

    public static UITitleBar build(Activity activity) {
        UITitleBar toolbar = new UITitleBar(activity.findViewById(R.id.tool_bar));
        Intent intent = activity.getIntent();
        toolbar.loadIntent(intent);
        int color = Util.getColorAccent(activity);
        toolbar.setBackgroundColor(color);
        return toolbar;
    }

    final String TAG = "UITitleBar";
    private final View mView;
    private ImageView backImageView;
    private TextView titleView;
    private TextView rightTextView;
    private ImageView rightImgView;
    public UITitleBar(View toolBarView) {
        mView = toolBarView;
        if (mView != null) {
            backImageView = mView.findViewById(R.id.ui_toolbar_back);
            titleView = mView.findViewById(R.id.ui_toolbar_title);
            rightTextView = mView.findViewById(R.id.ui_toolbar_right);
            rightImgView = mView.findViewById(R.id.ui_toolbar_right_img);
            if (backImageView != null && titleView != null) {
                L.i(TAG, "Init OK");
            }

        } else {
            L.i(TAG, "Error Not find Layout res");
        }
    }

    @Override
    public void loadIntent(Intent intent) {
        if(intent==null){
            return;
        }
        if(intent.hasExtra(KEY_TITLE)){
            setTitle(intent.getStringExtra(KEY_TITLE));
        }
    }

    @Override
    public void hideBack() {
        updateViewState(backImageView,View.GONE);
    }

    @Override
    public void showBack() {
        updateViewState(backImageView,View.VISIBLE);
    }

    @Override
    public void hideRight() {
        updateViewState(rightTextView,View.GONE);
    }

    @Override
    public void showRight() {
        updateViewState(rightTextView,View.VISIBLE);
    }

    @Override
    public void updateViewState(View v, int showAndHide) {
        if(v!=null){
            v.setVisibility(showAndHide);
        }
    }

    @Override
    public TextView getRightTextView() {
        return rightTextView;
    }

    @Override
    public ImageView getBackImageView() {
        return backImageView;
    }

    @Override
    public ImageView getRightImgView() {
        return rightImgView;
    }

    @Override
    public TextView getTitleView() {
        return titleView;
    }

    @Override
    public void setTitle(String title) {
        if (titleView != null) {
            titleView.setText(title);
            titleView.setSelected(true);
        }
    }

    @Override
    public void setTitle(int title) {
        if (titleView != null) {
            titleView.setText(title);
            titleView.setSelected(true);
        }
    }

    @Override
    public void setTitleColor(int color) {
        if(titleView!=null){
            titleView.setTextColor(color);
        }
    }

    @Override
    public void setTitleSize(int size) {
        if(titleView!=null){
            titleView.setTextSize(size);
        }
    }

    @Override
    public void setBackground(Drawable drawable) {
        if(mView!=null){
            mView.setBackground(drawable);
        }
    }

    @Override
    public void setBackgroundColor(int color) {
        if(mView!=null){
            mView.setBackgroundColor(color);
        }
    }

    @Override
    public void setBackgroundResource(int resId) {
        if(mView!=null){
            mView.setBackgroundResource(resId);
        }
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void setBackOnClickListener(View.OnClickListener onClickListener) {
        if (backImageView != null) {
            backImageView.setOnClickListener(v -> {
                if(onClickListener!=null){
                    onClickListener.onClick(v);
                }
            });
        }
    }

    @Override
    public void setRightOnClickListener(View.OnClickListener onClickListener) {
        if(rightTextView!=null){
            rightTextView.setOnClickListener(onClickListener);
        }
    }

    @Override
    public void setRightImgOnClickListener(View.OnClickListener onClickListener) {
        if(rightImgView!=null){
            rightImgView.setOnClickListener(onClickListener);
        }
    }

    @Override
    public void show() {
        updateViewState(mView,View.VISIBLE);
    }

    @Override
    public void hide() {
        updateViewState(mView,View.GONE);
    }
}
